{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 实践：宿舍人脸识别门禁系统\n",
    "> 1. 创建FaceSet\n",
    "> 2. 查询FaceSet\n",
    "> 3. 存入face_token数据\n",
    "> 4. 获取FaceSet信息数据\n",
    "> 5. 人脸搜索\n",
    "> 6. 人脸对比"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 创建FaceSet"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 描述\n",
    "> 1. 创建一个人脸的集合 FaceSet，用于存储人脸标识 face_token。一个 FaceSet 能够存储10000个 face_token。\n",
    "> 2. 试用API Key可以创建1000个FaceSet，正式API Key可以创建10000个FaceSet。\n",
    "* 调用URL\n",
    "> https://api-cn.faceplusplus.com/facepp/v3/faceset/create\n",
    "\n",
    "* 调用方法\n",
    "> POST\n",
    "\n",
    "* 请求参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>是否必选</th>\n",
       "      <th>参数名</th>\n",
       "      <th>类型</th>\n",
       "      <th>参数说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_key</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此 API 的 API Key</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_secret</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此 API 的 API Secret</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>可选</td>\n",
       "      <td>display_name</td>\n",
       "      <td>String</td>\n",
       "      <td>人脸集合的名字，最长256个字符，不能包括字符^@,&amp;=*'\"</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>可选</td>\n",
       "      <td>outer_id</td>\n",
       "      <td>String</td>\n",
       "      <td>账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>可选</td>\n",
       "      <td>tags</td>\n",
       "      <td>String</td>\n",
       "      <td>FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长255个字符，多个...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>可选</td>\n",
       "      <td>face_tokens</td>\n",
       "      <td>String</td>\n",
       "      <td>人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_t...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>可选</td>\n",
       "      <td>user_data</td>\n",
       "      <td>String</td>\n",
       "      <td>自定义用户信息，不大于16 KB，不能包括字符^@,&amp;=*'\"</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>可选</td>\n",
       "      <td>force_merge</td>\n",
       "      <td>Int</td>\n",
       "      <td>在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_to...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  是否必选           参数名      类型  \\\n",
       "0   必选       api_key  String   \n",
       "1   必选    api_secret  String   \n",
       "2   可选  display_name  String   \n",
       "3   可选      outer_id  String   \n",
       "4   可选          tags  String   \n",
       "5   可选   face_tokens  String   \n",
       "6   可选     user_data  String   \n",
       "7   可选   force_merge     Int   \n",
       "\n",
       "                                                参数说明  \n",
       "0                                  调用此 API 的 API Key  \n",
       "1                               调用此 API 的 API Secret  \n",
       "2                    人脸集合的名字，最长256个字符，不能包括字符^@,&=*'\"  \n",
       "3  账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255...  \n",
       "4  FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长255个字符，多个...  \n",
       "5  人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_t...  \n",
       "6                    自定义用户信息，不大于16 KB，不能包括字符^@,&=*'\"  \n",
       "7  在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_to...  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "pd.read_html(\"\"\"<table class=\"wrapped confluenceTable\"><colgroup><col><col><col><col></colgroup><tbody><tr><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">是否必选</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数名</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">类型</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数说明</span></p></th></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_key</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此 API 的 API Key</span></p></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_secret</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此 API 的 API Secret</span></p></td></tr><tr><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">display_name</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">人脸集合的名字，最长<span class=\"inline-comment-marker\" data-ref=\"9f098728-ac30-41a2-aab6-05b79f8a7d2e\">256</span>个字符，不能包括字符^@,&amp;=*'\"</span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">outer_id</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255个字符，<span class=\"inline-comment-marker\" data-ref=\"63fc0fd1-cd44-4256-bab5-971a939d66e2\"><span class=\"inline-comment-marker\" data-ref=\"183dcb11-c22c-47f4-aa9c-2ba6f3c8210c\">不能包括字符^@,&amp;=*'\"</span></span></span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">tags</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长<span class=\"inline-comment-marker\" data-ref=\"1be760b6-f20d-4ff3-a086-b7f763abd84d\">255</span>个字符，多个 tag 用逗号分隔，每个 tag 不能包括字符^@,&amp;=*'\"</span></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">可选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">face_tokens</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_token</span></p></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">user_data</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">自定义用户信息，不大于<span class=\"inline-comment-marker\" data-ref=\"f9edadb1-691d-4d80-933e-909096780f61\">16 KB，不能包括字符^@,&amp;=*'\"</span></span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">force_merge</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">Int</span></td><td colspan=\"1\" class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_token 加入已经存在的 FaceSet 中</span></p><p><span style=\"color: rgb(0,0,0);\">0：不将 face_tokens 加入已存在的 FaceSet 中，直接返回 FACESET_EXIST 错误</span></p><p><span style=\"color: rgb(0,0,0);\">1：将 face_tokens 加入已存在的 FaceSet 中</span></p><p><span style=\"color: rgb(0,0,0);\">默认值为0</span></p></td></tr></tbody></table>\"\"\")[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备工作\n",
    "import requests\n",
    "API_key = 'fM1G06dF-zEj3SO2kHxvHjF8ZoMHivBl'\n",
    "API_secret = '4lq5-_IsVbNsykzVWdLKwn2BeQZDOYFA'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# A-1 创建人脸数据集合\n",
    "FaceSet_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret\n",
    "}\n",
    "r = requests.post(url=FaceSet_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '2ff384d98a629de6b4f3fa039fd4675c',\n",
       " 'time_used': 142,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1647950015,70117bf1-8074-47cc-b6ed-637db0bf6854',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2ff384d98a629de6b4f3fa039fd4675c'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "faceset_token = r.json()['faceset_token']\n",
    "faceset_token"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " ## 2. 查询FaceSet\n",
    "* 描述\n",
    "> 1. 获取一个 FaceSet 的所有信息，包括此 FaceSet 的 faceset_token, outer_id, display_name 的信息，以及此 FaceSet 中存放的 face_token 数量与列表。\n",
    "> 2. 注意：2017年8月16日后，调用本接口将不会一次性返回全部的 face_token 列表。单次查询最多返回 100 个 face_token。如需获取全量数据，需要配合使用 start 和 next 参数。请尽快修改调整您的程序\n",
    "* 调用URL\n",
    "> https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\n",
    "\n",
    "* 调用方法\n",
    "> POST\n",
    "\n",
    "* 调用参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>是否必选</th>\n",
       "      <th>参数名</th>\n",
       "      <th>类型</th>\n",
       "      <th>参数说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_key</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此API的API Key</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_secret</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此API的API Secret</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>必选(二选一)</td>\n",
       "      <td>faceset_token</td>\n",
       "      <td>String</td>\n",
       "      <td>FaceSet的标识</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>必选(二选一)</td>\n",
       "      <td>outer_id</td>\n",
       "      <td>String</td>\n",
       "      <td>用户提供的FaceSet标识</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>可选（2017年8月16日起可使用）</td>\n",
       "      <td>start</td>\n",
       "      <td>Int</td>\n",
       "      <td>一个数字 n，表示开始返回的 face_token 在本 FaceSet 中的序号， n 是...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 是否必选            参数名      类型  \\\n",
       "0                  必选        api_key  String   \n",
       "1                  必选     api_secret  String   \n",
       "2             必选(二选一)  faceset_token  String   \n",
       "3             必选(二选一)       outer_id  String   \n",
       "4  可选（2017年8月16日起可使用）          start     Int   \n",
       "\n",
       "                                                参数说明  \n",
       "0                                     调用此API的API Key  \n",
       "1                                  调用此API的API Secret  \n",
       "2                                         FaceSet的标识  \n",
       "3                                     用户提供的FaceSet标识  \n",
       "4  一个数字 n，表示开始返回的 face_token 在本 FaceSet 中的序号， n 是...  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_html(\"\"\"<div class=\"table-wrap\"><table class=\"wrapped confluenceTable\"><colgroup><col><col><col><col></colgroup><tbody><tr><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">是否必选</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数名</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">类型</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数说明</span></p></th></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_key</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此API的API Key</span></p></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_secret</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此API的API Secret</span></p></td></tr><tr><td rowspan=\"2\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">必选(二选一)</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">faceset_token</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">FaceSet的标识</span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">outer_id</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">用户提供的FaceSet标识</span></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">可选</span></p><p><span style=\"color: rgb(0,0,0);\">（2017年8月16日起可使用）</span></p></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">start</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">Int</span></td><td colspan=\"1\" class=\"confluenceTd\"><p class=\"p1\"><span style=\"color: rgb(0,0,0);\">一个数字 n，表示开始返回的 face_token 在本 FaceSet 中的序号， n 是 [1,10000] 间的一个整数。 </span></p><p class=\"p1\"><span style=\"color: rgb(0,0,0);\">通过传入数字 n，可以控制本 API 从第 n 个 <span class=\"s1\">face_token </span>开始返回。返回的 face_token 按照创建时间排序，每次返回 <span class=\"s1\">100 </span>个 <span class=\"s1\">face_token</span>。</span></p><p class=\"p1\"><span style=\"color: rgb(0,0,0);\">默认值为 1。</span></p><p class=\"p1\"><span style=\"color: rgb(0,0,0);\">您可以输入上一次请求本 API 返回的 next 值，用以获得接下来的 100 个 face_token。</span></p></td></tr></tbody></table></div>\"\"\")[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "getdetail_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail'\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'faceset_token':faceset_token\n",
    "}\n",
    "r = requests.post(url=getdetail_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '2ff384d98a629de6b4f3fa039fd4675c',\n",
       " 'request_id': '1647952391,d8fc0d73-7f98-4bb0-b4c9-0ddb570b718f',\n",
       " 'time_used': 75,\n",
       " 'outer_id': 'API021214'}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. 更新数据集描述信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "update_FaceSet_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/update'\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'faceset_token':faceset_token,\n",
    "    'new_outer_id':'API021214',\n",
    "    'display_name':'20级网新API周二晚班',\n",
    "    'user_data':'59人，23男同学，36女同学'\n",
    "}\n",
    "r = requests.post(url=update_FaceSet_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '2ff384d98a629de6b4f3fa039fd4675c',\n",
       " 'request_id': '1647952391,d8fc0d73-7f98-4bb0-b4c9-0ddb570b718f',\n",
       " 'time_used': 75,\n",
       " 'outer_id': 'API021214'}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. 增加人脸入数据集合"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. 人脸检测（功能）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_key = 'fM1G06dF-zEj3SO2kHxvHjF8ZoMHivBl'\n",
    "API_secret = '4lq5-_IsVbNsykzVWdLKwn2BeQZDOYFA'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module detect:\n",
      "\n",
      "NAME\n",
      "    detect\n",
      "\n",
      "FUNCTIONS\n",
      "    face_detect(API_key, API_secret, image_path)\n",
      "        该函数为调用face++ face_detect接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\玖玖\\python\\2022\\detect.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(detect)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648479840,eeb7f02a-c252-4221-8196-9d7f0023d943',\n",
       " 'time_used': 94,\n",
       " 'faces': [{'face_token': '8b9d44348d6797888effde20a9b7339c',\n",
       "   'face_rectangle': {'top': 98, 'left': 164, 'width': 118, 'height': 118}}],\n",
       " 'image_id': '3XXAy6wuwDnc0I+ASLvc3Q==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_key,API_secret,'Liu_01.png')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 人脸分析（功能）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module analyze:\n",
      "\n",
      "NAME\n",
      "    analyze\n",
      "\n",
      "FUNCTIONS\n",
      "    face_analyze(API_key, API_secret, face_tokens, attributes=None, landmark=1)\n",
      "        该函数为调用face++ face_detect接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\玖玖\\python\\2022\\analyze.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(analyze)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "face1 = detect.face_detect(API_key,API_secret,'Liu_01.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'6a0900a1101ec4667ccdfbfb1223c774'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_tokens1 = face1[\"faces\"][0][\"face_token\"]\n",
    "face_tokens1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 82,\n",
       " 'request_id': '1648480049,fb0aee70-ad90-4e79-a05e-6530e43ecf3a',\n",
       " 'faces': [{'landmark': {'mouth_upper_lip_left_contour2': {'y': 174, 'x': 199},\n",
       "    'mouth_upper_lip_top': {'y': 171, 'x': 214},\n",
       "    'mouth_upper_lip_left_contour1': {'y': 171, 'x': 208},\n",
       "    'left_eye_upper_left_quarter': {'y': 123, 'x': 179},\n",
       "    'left_eyebrow_lower_middle': {'y': 112, 'x': 178},\n",
       "    'mouth_upper_lip_left_contour3': {'y': 175, 'x': 202},\n",
       "    'right_eye_top': {'y': 115, 'x': 237},\n",
       "    'left_eye_bottom': {'y': 127, 'x': 186},\n",
       "    'right_eyebrow_lower_left_quarter': {'y': 105, 'x': 231},\n",
       "    'right_eye_pupil': {'y': 118, 'x': 236},\n",
       "    'mouth_lower_lip_right_contour1': {'y': 180, 'x': 230},\n",
       "    'mouth_lower_lip_right_contour3': {'y': 186, 'x': 228},\n",
       "    'mouth_lower_lip_right_contour2': {'y': 180, 'x': 237},\n",
       "    'contour_chin': {'y': 218, 'x': 219},\n",
       "    'contour_left9': {'y': 215, 'x': 205},\n",
       "    'left_eye_lower_right_quarter': {'y': 126, 'x': 191},\n",
       "    'mouth_lower_lip_top': {'y': 184, 'x': 215},\n",
       "    'right_eyebrow_upper_middle': {'y': 96, 'x': 242},\n",
       "    'left_eyebrow_left_corner': {'y': 121, 'x': 167},\n",
       "    'right_eye_bottom': {'y': 120, 'x': 237},\n",
       "    'contour_left7': {'y': 199, 'x': 185},\n",
       "    'contour_left6': {'y': 189, 'x': 176},\n",
       "    'contour_left5': {'y': 178, 'x': 170},\n",
       "    'contour_left4': {'y': 167, 'x': 167},\n",
       "    'contour_left3': {'y': 154, 'x': 165},\n",
       "    'contour_left2': {'y': 142, 'x': 164},\n",
       "    'contour_left1': {'y': 130, 'x': 164},\n",
       "    'left_eye_lower_left_quarter': {'y': 128, 'x': 180},\n",
       "    'contour_right1': {'y': 118, 'x': 277},\n",
       "    'contour_right3': {'y': 146, 'x': 280},\n",
       "    'contour_right2': {'y': 132, 'x': 279},\n",
       "    'mouth_left_corner': {'y': 179, 'x': 192},\n",
       "    'contour_right4': {'y': 160, 'x': 280},\n",
       "    'contour_right7': {'y': 197, 'x': 260},\n",
       "    'right_eyebrow_left_corner': {'y': 105, 'x': 220},\n",
       "    'nose_right': {'y': 151, 'x': 230},\n",
       "    'nose_tip': {'y': 152, 'x': 208},\n",
       "    'contour_right5': {'y': 174, 'x': 277},\n",
       "    'nose_contour_lower_middle': {'y': 162, 'x': 211},\n",
       "    'left_eyebrow_lower_left_quarter': {'y': 116, 'x': 172},\n",
       "    'mouth_lower_lip_left_contour3': {'y': 189, 'x': 205},\n",
       "    'right_eye_right_corner': {'y': 118, 'x': 247},\n",
       "    'right_eye_lower_right_quarter': {'y': 120, 'x': 243},\n",
       "    'mouth_upper_lip_right_contour2': {'y': 169, 'x': 232},\n",
       "    'right_eyebrow_lower_right_quarter': {'y': 106, 'x': 251},\n",
       "    'left_eye_left_corner': {'y': 127, 'x': 175},\n",
       "    'mouth_right_corner': {'y': 172, 'x': 243},\n",
       "    'mouth_upper_lip_right_contour3': {'y': 172, 'x': 229},\n",
       "    'right_eye_lower_left_quarter': {'y': 121, 'x': 232},\n",
       "    'left_eyebrow_right_corner': {'y': 109, 'x': 195},\n",
       "    'left_eyebrow_lower_right_quarter': {'y': 111, 'x': 187},\n",
       "    'right_eye_center': {'y': 119, 'x': 237},\n",
       "    'nose_left': {'y': 155, 'x': 193},\n",
       "    'mouth_lower_lip_left_contour1': {'y': 183, 'x': 202},\n",
       "    'left_eye_upper_right_quarter': {'y': 122, 'x': 191},\n",
       "    'right_eyebrow_lower_middle': {'y': 104, 'x': 241},\n",
       "    'left_eye_top': {'y': 121, 'x': 185},\n",
       "    'left_eye_center': {'y': 125, 'x': 186},\n",
       "    'contour_left8': {'y': 207, 'x': 194},\n",
       "    'contour_right9': {'y': 214, 'x': 235},\n",
       "    'right_eye_left_corner': {'y': 121, 'x': 227},\n",
       "    'mouth_lower_lip_bottom': {'y': 189, 'x': 216},\n",
       "    'left_eyebrow_upper_left_quarter': {'y': 111, 'x': 169},\n",
       "    'left_eye_pupil': {'y': 124, 'x': 185},\n",
       "    'right_eyebrow_upper_left_quarter': {'y': 97, 'x': 229},\n",
       "    'contour_right8': {'y': 206, 'x': 248},\n",
       "    'right_eyebrow_right_corner': {'y': 110, 'x': 260},\n",
       "    'right_eye_upper_left_quarter': {'y': 117, 'x': 231},\n",
       "    'left_eyebrow_upper_middle': {'y': 105, 'x': 176},\n",
       "    'right_eyebrow_upper_right_quarter': {'y': 100, 'x': 253},\n",
       "    'nose_contour_left1': {'y': 126, 'x': 202},\n",
       "    'nose_contour_left2': {'y': 145, 'x': 198},\n",
       "    'mouth_upper_lip_right_contour1': {'y': 169, 'x': 219},\n",
       "    'nose_contour_right1': {'y': 124, 'x': 219},\n",
       "    'nose_contour_right2': {'y': 143, 'x': 225},\n",
       "    'mouth_lower_lip_left_contour2': {'y': 185, 'x': 198},\n",
       "    'contour_right6': {'y': 186, 'x': 270},\n",
       "    'nose_contour_right3': {'y': 158, 'x': 221},\n",
       "    'nose_contour_left3': {'y': 160, 'x': 201},\n",
       "    'left_eye_right_corner': {'y': 126, 'x': 197},\n",
       "    'left_eyebrow_upper_right_quarter': {'y': 104, 'x': 186},\n",
       "    'right_eye_upper_right_quarter': {'y': 116, 'x': 242},\n",
       "    'mouth_upper_lip_bottom': {'y': 174, 'x': 214}},\n",
       "   'face_rectangle': {'width': 118, 'top': 98, 'left': 164, 'height': 118},\n",
       "   'face_token': '6a0900a1101ec4667ccdfbfb1223c774'}]}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "analyze.face_analyze(API_key,API_secret,face_tokens1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. 人脸对比（功能）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module compare:\n",
      "\n",
      "NAME\n",
      "    compare\n",
      "\n",
      "FUNCTIONS\n",
      "    face_compare(API_key, API_secret, face_token1, face_token2)\n",
      "        该函数为调用face++ face_detect接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\玖玖\\python\\2022\\compare.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(compare)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'detect' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-10-c0df46c85e2a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mface2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdetect\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mface_detect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mAPI_key\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mAPI_secret\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"IMG_4923.JPG\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'detect' is not defined"
     ]
    }
   ],
   "source": [
    "face2 = detect.face_detect(API_key,API_secret,\"IMG_4923.JPG\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'face2' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-9-7f1c8d7d8b97>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mface_tokens2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mface2\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"faces\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"face_token\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mface_tokens2\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'face2' is not defined"
     ]
    }
   ],
   "source": [
    "face_tokens2 = face2[\"faces\"][0][\"face_token\"]\n",
    "face_tokens2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
